/**
 * @Title: aritificialSelect_ctrl.js
 * @Description: TODO
 * Copyright: Copyright (c) 2015 
 * Company:中国平安
 *
 * @Project HIMS-FWA
 * @author ex-xieqi001
 * @date 2015年1月27日 上午10:01:57
 * @version V1.0
 * 
 * 

 */
;
(function(){
	
	'use strict';
	
	var app = angular.module('fwa.directives');
	app.directive('aritificialSelect', ['BaseURL','$parse','$timeout', function(baseUrl,$parse,$timeout) {
		//style="position: absolute;width: 100%;top: 28px;padding: 4px;background-color: #fff;border: 1px #d1d1d1 solid;z-index: 1000000;"
		var template = '<div class="full-width full-height" ng-focus="focusShow();" ng-blur="cancel();" tabindex="-1" >' +
							'<div class="full-width" ng-click="dropdownClick();">' +
							'<input class="form-control" type="text"  style="float: left;" ng-model="value" ng-blur="cancel();">' +
							'<span class="caret" style="margin-left: -18px;margin-top: 10px;"></span>' +
							'</div>' +
							'<div class="dropdownlist full-overflow-y" ng-show="dropdownShow" ng-style="{\'width\': width}" style="max-height: 200px;" > '+
								'<div class="col-xs-12" ng-class="{\'select-results\': data.selected}" style="line-height: 28px;" ng-repeat="data in containerDatas" ng-click="selectData(data);"> ' +
									'<span ng-bind="data[showName]"></span>' +
								'</div>' +
							'</div>' +
						'</div>';
		function linker($scope, elm, attrs, ctrl) {
			
			//显示的对象属性名称
			$scope.showName = $scope.showName || "name";
			
			//返回的对象属性名称
			$scope.resultName = $scope.resultName || "code";
			
			//是否显示
			$scope.dropdownShow = false;
			
			//下拉列表集合
			$scope.containerDatas = [];
			
			$scope.$watch('containerData',function(v){
				if (v) {
					$scope.containerDatas = v;
				}
				else {
					$scope.containerDatas = [];
				}
			});
			$scope.$watch('value',function(v) {
				if (isFlag) {
					isFlag = false;
					$scope.result = selectValue;
				}
				else {
					$scope.result = v;
					selectData();
				}
			});
			
			$scope.$watch('result',function(v) {
				if (v && typeof(v) == 'object') {
					$scope.selectData(v);
				}
				else {
					if (!v) {
						selectData();
						$scope.value = '';
						selectValue = '';
					}
				}
			});
			
			//点击事件
			$scope.dropdownClick = function () {
				isShow = false;
				$scope.dropdownShow = !$scope.dropdownShow;
				if ($scope.dropdownShow) {
					$scope.width = elm.get(0).offsetWidth;
				}
			}
			
			//选中还是手工录入的值
			var isFlag = false;
			
			//选中时返回的值
			var selectValue = '';
			function selectData(data) {
				angular.forEach($scope.containerDatas,function(v,k){
					if (data && data == v) {
						v.selected = true;
					}
					else {
						v.selected = false;
					}
				});
			}
			//选中值
			$scope.selectData = function (data) {
				isFlag = true;
				selectData(data);
				$scope.value = data[$scope.showName];
				
				selectValue = data[$scope.resultName];
				
				$scope.dropdownShow = false;
			}
			
			//是否还在下拉框内
			var isShow = true;
			
			//是否展示下拉框事件  获取焦点事件
			$scope.focusShow = function() {
				isShow = false;
				$scope.dropdownShow = true;
			}
			
			//隐藏下拉框 失去焦点事件
			$scope.cancel = function() {
				isShow = true;
				$timeout(function() {
					if (isShow) {
						$scope.dropdownShow = false;
					}
				}, 200);
			}
			
		}
		return {
			template: template,
			restrict: 'E',
			scope:{
				showName: '@showName',
				resultName: '@resultName',
				containerData: '=containerData',
				result: '=result'
			},
			replace: true,
			link: linker
		};
	}]);
	
})();